﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebSite_that_use_Wizard.NET
{
    public partial class Application : System.Web.UI.Page
    {
        private Wizard.Asp.AspEngine mWizardEngine;
        private Wizard.IDataStoreContext mDataStoreContext;
        private Wizard.Source.IWizardApplication mApplication;
        static int ApplicationSeq;
        int ApplicationSeqNo;
        
        public Application()
        {
            ApplicationSeq++;
            ApplicationSeqNo = ApplicationSeq;
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            mWizardEngine = (Wizard.Asp.AspEngine)Application["WizardEngine"];
            mDataStoreContext = mWizardEngine.DataStore.GetNewContext();
            mApplication = mDataStoreContext.GetApplication(int.Parse(Request.QueryString["id"]));
        }

        protected void ViewSourceFile_Click(object sender, EventArgs e)
        {
            string SourceFileId = ((LinkButton)sender).CommandArgument;
            Response.Redirect("SourceFile.aspx?id=" + SourceFileId);
        }

        protected void DeleteSourceFile_Click(object sender, EventArgs e)
        {
            string SourceFileId = ((LinkButton)sender).CommandArgument;
            mApplication.DeleteSourceFile(mDataStoreContext, int.Parse(SourceFileId));

        }

        protected void lnkCreateNewSourceFile_Click(object sender, EventArgs e)
        {
            Wizard.Source.ISourceFile newApp = mApplication.NewSourceFile(mDataStoreContext, "New File");
            Response.Redirect("SourceFile.aspx?id=" + newApp.SourceFileId.ToString());
        }

        protected void Run_Click(object sender, EventArgs e)
        {
            Wizard.Runtime.IClassInstance appInstance = mApplication.Instantiate(mDataStoreContext);
            Response.Redirect("Run.aspx?application=" + appInstance.ClassInstanceId.ToString());
        }

        protected void Compile_Click(object sender, EventArgs e)
        {
            try
            {
                mWizardEngine.CompilerEngine.Compile(mDataStoreContext, mApplication);
                lblError.Text = "Compilation succeeded";
                mDataStoreContext.SaveAllChanges();
            }
            catch (Exception ex)
            {
                lblError.Text = ex.ToString();
            }
        }


        public class ApplicationPageHelper
        {
            internal Application mApplicationPage;

            public ApplicationPageHelper()
            {
            }

            public Wizard.Source.IWizardApplication GetApplication(int queryApplicationId)
            {
                var result = mApplicationPage.mDataStoreContext.GetApplication(queryApplicationId);
                return result;
            }

            public Wizard.Source.ISourceFile[] GetSourceFiles(int queryApplicationId)
            {
                var method1 = mApplicationPage.mDataStoreContext.GetApplication(queryApplicationId).GetSourceFiles();
                if (method1.Length > 0) return method1;
                var method2 = mApplicationPage.mDataStoreContext.GetSourceFiles(queryApplicationId);
                return method2;
            }

            public void Update(int queryApplicationId, string applicationName)
            {
                var result = mApplicationPage.mDataStoreContext.GetApplication(queryApplicationId);
                result.Update(mApplicationPage.mDataStoreContext, applicationName);
            }

            public Wizard.Source.IClassSource[] GetClasses(int queryApplicationId)
            {
                var method2 = mApplicationPage.mDataStoreContext.GetClasses(queryApplicationId);
                return method2;
            }

            public void UpdateClass(int queryApplicationId, string applicationName)
            {
                var result = mApplicationPage.mDataStoreContext.GetApplication(queryApplicationId);
                result.UpdateClass(mApplicationPage.mDataStoreContext, applicationName);
            }


        }

        protected void ObjectDataSource_ObjectCreated(object sender, ObjectDataSourceEventArgs e)
        {
            var helper = e.ObjectInstance as ApplicationPageHelper;
            helper.mApplicationPage = this;
        }
    }
}
